package com.yhl;

import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import static com.sun.deploy.net.proxy.ProxyType.NONE;
import static java.sql.Types.NUMERIC;
import static jdk.nashorn.internal.runtime.regexp.joni.encoding.CharacterType.BLANK;
import static jdk.nashorn.internal.runtime.regexp.joni.encoding.CharacterType.S;
import static org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType.FORMULA;
import static org.apache.xmlbeans.impl.piccolo.xml.Piccolo.STRING;

/**
 * @ClassName ExcelReadTest
 * @Description TODO
 * @Author yao
 * @Date 2024/2/22  17:16
 **/
public class ExcelReadTest {
    String path = "C:\\Users\\Administrator\\Desktop\\Note\\smbms_-wx_-parent\\POI\\";

    @Test
    public void testRead03() throws Exception {
        FileInputStream fileInputStream = new FileInputStream(path + "林北星.xls");

        Workbook workbook = new HSSFWorkbook(fileInputStream);

        Sheet sheet = workbook.getSheetAt(0);

        for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
                Cell cell = row.getCell(cellNum);
                //getStringCellValue获取字符串类型
                System.out.print(cell.getStringCellValue() + ",");
            }
        }

        fileInputStream.close();
    }


    @Test
    public void testRead07() throws Exception {
        FileInputStream fileInputStream = new FileInputStream(path + "张万森.xlsx");

        Workbook workbook = new XSSFWorkbook(fileInputStream);

        Sheet sheet = workbook.getSheetAt(0);

        for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
                Cell cell = row.getCell(cellNum);
                //getStringCellValue获取字符串类型
                System.out.print(cell.getStringCellValue() + ",");
            }
        }

        fileInputStream.close();
    }

    @Test
    public void testCellType() throws Exception {
        FileInputStream fileInputStream = new FileInputStream(path + "cellType.xlsx");
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        Row rowTitle = sheet.getRow(0);
        if (rowTitle != null) {
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                Cell cell = rowTitle.getCell(cellNum);
                if (cell != null) {
                    int cellType = cell.getCellType();
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue + " | ");
                }
            }
            System.out.println();
        //获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            if (rowData != null) {
                //读取列
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
//                    System.out.print("[" + (rowNum + 1) + "-" + (cellNum + 1)+"]");
                    Cell cell = rowData.getCell(cellNum);
                    //匹配列的数据类型
                    if (cell != null) {
                        int cellType = cell.getCellType();
                        String cellValue = "";
                        switch (cellType) {
                            //字符串时
                            case Cell.CELL_TYPE_STRING:
//                                System.out.print("【String】");
                                cellValue = cell.getStringCellValue();
                                break;
                            //boolean类型时
                            case Cell.CELL_TYPE_BOOLEAN:
//                                System.out.print("【BOOLEAN】");
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                break;
                            //为空时
                            case Cell.CELL_TYPE_BLANK:
//                                System.out.print("【BLANK】");
                                break;
                            //为数字时
                            case Cell.CELL_TYPE_NUMERIC:
//                                System.out.print("【NUMERIC】");
                                //为日期时
                                if (DateUtil.isCellDateFormatted(cell)) {
//                                    System.out.print("【日期】");
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                    //不是日期的数字
                                } else {
                                    //不是日期格式，防止数字过长
//                                    System.out.print("【将普通数字转换成字符串】");
                                    cell.setCellType(Cell.CELL_TYPE_STRING);
                                    cellValue = cell.toString();
                                }
                                break;
                            case Cell.CELL_TYPE_ERROR:
//                                System.out.print("【数据类型错误】");
                                break;
                        }
                        System.out.print(cellValue + "|");
                    }
                }
                System.out.println();
            }
        }
        }
        fileInputStream.close();
    }

    @Test
    public void testFormula() throws Exception {
        FileInputStream fileInputStream = new FileInputStream(path + "cellType.xlsx");
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(1);
        Row row = sheet.getRow(5);
        Cell cell = row.getCell(0);
        FormulaEvaluator formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
        int cellType = cell.getCellType();
        switch (cellType){
            case Cell.CELL_TYPE_FORMULA:
                String formula = cell.getCellFormula();
                System.out.println(formula);

                //计算
                CellValue evaluate = formulaEvaluator.evaluate(cell);

                String cellValue = evaluate.formatAsString();
                System.out.println(cellValue);
                break;
        }
        fileInputStream.close();
    }


}
